// Plugin: jQuery.dragmove
// Source: github.com/nathco/jQuery.dragmove
// Author: Nathan Rutzky
// Update: 1.0

(function($) {

    $.fn.dragmove = function() {
    
        return this.each(function() {
    
            var $document = $(document),
                $this = $(this),
                active,
                startX,
                startY;
            
            $this.on('mousedown touchstart', function(e) {
                active = true;
                startX = e.originalEvent.pageX - $this.offset().left;
                startY = e.originalEvent.pageY - $this.offset().top;
                //alert(startX + "=" + e.originalEvent.pageX + "-" + $this.offset().left)
                //alert(startY + "=" + e.originalEvent.pageY + "-" + $this.offset().top)
                if ('mousedown' == e.type){
                    click = $this;
                }



                if ('touchstart' == e.type){

                    touch = $this;
                }

                if (window.mozInnerScreenX == null){
                    event.preventDefault();
                    return false;
                }
            });
            
            $document.on('mousemove touchmove', function(e) {
                event.preventDefault();
                if ('mousemove' == e.type && active){
                
                    click.offset({ 
                    
                        left: e.originalEvent.pageX - startX,
                        top: e.originalEvent.pageY - startY 
                    
                    });
                    alert("left1" + "=" + (e.originalEvent.pageX - startX))
                    alert("top1" + "=" + (e.originalEvent.pageY - startY))
                }
                
                if ('touchmove' == e.type && active){

                    alert(2)
                    touch.offset({
                        left: e.originalEvent.pageX - startX,
                        top: e.originalEvent.pageY - startY
                    });
                    alert("left2" + "=" + (e.originalEvent.pageX - startX))
                    alert("top2" + "=" + (e.originalEvent.pageY - startY))
                }
                //alert("pageX"+e.originalEvent.pageX)
                //alert("startX"+startX)

            }).on('mouseup touchend', function() {
                active = false;
                
            });
                                
        });
            
    };

})(jQuery);